package www.wen.com.common.lib.util;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONObject;

import java.math.BigDecimal;

public class JsonUtil {

    public static String optString(JSONObject json, String key){
        if (json.isNull(key) || json.optString(key).equals("null"))
            return "";
        else
            return json.optString(key, "");
    }

    public static String optBigDecimal(JSONObject json, String key){
        if (json.isNull(key) || json.optString(key).equals("null"))
            return "0";
        else
            try{
                return new BigDecimal(json.optString(key)).toPlainString();
            }catch (Exception e){
                e.printStackTrace();
                Log.e("---->>",e.getMessage());
                return "0";
            }
    }

    public static Boolean optBoolean(String str){
        if (TextUtils.isEmpty(str) || str.equals("null"))
            return false;
        else
            return str.equals("true");
    }

}
